//
//  CYGeRenZhuYeTableViewCell.m
//  cyydt
//
//  Created by chu on 2019/1/29.
//  Copyright © 2019 chu. All rights reserved.
//

#import "CYGeRenZhuYeTableViewCell.h"

@implementation CYGeRenZhuYeTableViewCell

+ (instancetype)initWithTableView:(UITableView *)tableView Identifier:(NSString *)identifier{
    CYGeRenZhuYeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[NSBundle mainBundle] loadNibNamed:@"CYGeRenZhuYeTableViewCell" owner:self options:nil][0];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)setModel:(CYProjectModel *)model{
    _model = model;
    self.coverImageViewWidthConstraint.constant = 50;
    self.bofangImageViewWidthConstraint.constant = 50;
    self.titleLabelMarginTopConstraint.constant = 15;
    self.contentViewHeightConstraint.constant = 50;
    self.contentViewMarginTopConstraint.constant = 15;
    self.coverImageViewWidthConstraint.constant = 50;
    self.bofangImageViewWidthConstraint.constant = 50;
    
    if ([model.falg integerValue] == 2) {
        self.bofangImageView.hidden = YES;
        self.wenzhangImageView.hidden = YES;
        //回答了
        [self.headImageView sd_setImageWithURL:[NSURL URLWithString:model.answer_avatar] placeholderImage:[UIImage imageWithColor:[UIColor lightGrayColor] Size:self.headImageView.frame.size]];
        self.nicknameLabel.text = model.answer_nickname;
        self.typeLabel.text = @"回答了问题";
        self.timeLabel.text = model.answerTime;
        
        CGFloat contentHeight = [[Tool sharedInstance] heightForString:[[Tool sharedInstance] delHTMLBiqoQianWithHtml:model.answer_content] andWidth:kWindowW - 30 fontSize:16];
        NSArray *lines = [[Tool sharedInstance] getLinesArrayOfStringWithString:[[Tool sharedInstance] delHTMLBiqoQianWithHtml:model.answer_content] Font:[UIFont systemFontOfSize:16] Rect:CGRectMake(0, 0, kWindowW - 30, MAXFLOAT)];
        if (lines.count >= 2) {
            self.titleLabelHeightConstraint.constant = 45;
        }else{
            self.titleLabelHeightConstraint.constant = contentHeight;
        }
        self.titleLabel.text = [[Tool sharedInstance] delHTMLBiqoQianWithHtml:model.answer_content];
        
        self.coverImageViewWidthConstraint.constant = 0;
        self.bofangImageViewWidthConstraint.constant = 0;
        
        
        self.neirongLabel.text = model.title;
        
        self.zanLabel.text = [NSString stringWithFormat:@"%@点赞", model.like_number];
        self.pinglunLabel.text = [NSString stringWithFormat:@"%@回复", model.comment_number];
        
        [self addSeeMoreButton];

        
    }else if ([model.falg integerValue] == 3){
        //评论了
        [self.headImageView sd_setImageWithURL:[NSURL URLWithString:model.comment_avatar] placeholderImage:[UIImage imageWithColor:[UIColor lightGrayColor] Size:self.headImageView.frame.size]];
        self.nicknameLabel.text = model.comment_nickname;
        self.timeLabel.text = model.created_time;
        
        if ([model.type integerValue] == 7) {
            self.typeLabel.text = @"评论了文章";
            [self.bofangImageView sd_setImageWithURL:[NSURL URLWithString:model.image] placeholderImage:[UIImage imageNamed:@"wd_gezl_lj"]];
            self.wenzhangImageView.hidden = YES;
            self.neirongLabel.text = model.title;

        }else if ([model.type integerValue] == 28){
            self.typeLabel.text = @"评论了视频";
            self.wenzhangImageView.hidden = YES;
            [self.bofangImageView sd_setImageWithURL:[NSURL URLWithString:model.cover_url] placeholderImage:[UIImage imageNamed:@"wd_gezl_lj"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
                self.wenzhangImageView.hidden = NO;
            }];
            self.neirongLabel.text = model.title;

        }else if ([model.type integerValue] == 41){
            self.typeLabel.text = @"评论了回答";
            self.wenzhangImageView.hidden = YES;
            self.bofangImageView.hidden = YES;
            self.coverImageViewWidthConstraint.constant = 0;
            self.bofangImageViewWidthConstraint.constant = 0;
            self.neirongLabel.text = [[Tool sharedInstance] delHTMLBiqoQianWithHtml:model.content];

        }
        
        CGFloat contentHeight = [[Tool sharedInstance] heightForString:model.commentContent andWidth:kWindowW - 30 fontSize:16];
        NSArray *lines = [[Tool sharedInstance] getLinesArrayOfStringWithString:model.commentContent Font:[UIFont systemFontOfSize:16] Rect:CGRectMake(0, 0, kWindowW - 30, MAXFLOAT)];
        if (lines.count >= 2) {
            self.titleLabelHeightConstraint.constant = 45;
        }else{
            self.titleLabelHeightConstraint.constant = contentHeight;
        }
        self.titleLabel.text = model.commentContent;

        
        self.zanLabel.text = [NSString stringWithFormat:@"%@点赞", model.like_number];
        self.pinglunLabel.text = [NSString stringWithFormat:@"%@评论", model.comment_number];
        
        [self addSeeMoreButton];
        
        
    }else{
        [self.headImageView sd_setImageWithURL:[NSURL URLWithString:model.avatar] placeholderImage:[UIImage imageWithColor:[UIColor lightGrayColor] Size:self.headImageView.frame.size]];

        if ([model.type integerValue] == 7) {
            self.titleLabelHeightConstraint.constant = 0;
            self.titleLabelMarginTopConstraint.constant = 0;
            self.contentViewHeightConstraint.constant = 50;
            self.contentViewMarginTopConstraint.constant = 15;
            self.wenzhangImageView.hidden = YES;
            self.bofangImageView.hidden = NO;
            
            self.typeLabel.text = @"发布了文章";
            [self.bofangImageView sd_setImageWithURL:[NSURL URLWithString:model.image] placeholderImage:[UIImage imageNamed:@"wd_gezl_lj"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            }];
            self.neirongLabel.text = model.title;
            self.nicknameLabel.text = model.nickname;
            self.timeLabel.text = model.created_time;
            self.zanLabel.text = [NSString stringWithFormat:@"%@点赞", model.like_number];
            self.pinglunLabel.text = [NSString stringWithFormat:@"%@评论", model.comment_number];
        }else if ([model.type integerValue] == 3){
            self.titleLabelHeightConstraint.constant = 0;
            self.titleLabelMarginTopConstraint.constant = 0;
            self.contentViewHeightConstraint.constant = 50;
            self.contentViewMarginTopConstraint.constant = 15;
            self.wenzhangImageView.hidden = YES;
            self.bofangImageView.hidden = NO;
            
            self.typeLabel.text = @"发布了项目";
            self.wenzhangImageView.hidden = YES;
            [self.bofangImageView sd_setImageWithURL:[NSURL URLWithString:model.image] placeholderImage:[UIImage imageNamed:@"wd_gezl_lj"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            }];
            self.neirongLabel.text = model.title;
            self.nicknameLabel.text = model.nickname;
            self.timeLabel.text = model.created_time;
            self.zanLabel.text = [NSString stringWithFormat:@"%@收藏", model.collect_number];
        }else if ([model.type integerValue] == 4){
            CGFloat contentHeight = [[Tool sharedInstance] heightForString:model.title andWidth:kWindowW - 30 fontSize:16];

            self.titleLabelHeightConstraint.constant = contentHeight;
            self.titleLabelMarginTopConstraint.constant = 15;
            self.contentViewHeightConstraint.constant = 0;
            self.contentViewMarginTopConstraint.constant = 0;
            self.wenzhangImageView.hidden = YES;
            self.bofangImageView.hidden = YES;
            
            self.typeLabel.text = @"发布了问题";
            self.titleLabel.text = model.title;
            self.nicknameLabel.text = model.nickname;
            self.timeLabel.text = model.created_time;
            self.zanLabel.text = [NSString stringWithFormat:@"%@回答", model.answer_number];
            
        }else if ([model.type integerValue] == 28){
            self.titleLabelHeightConstraint.constant = 0;
            self.titleLabelMarginTopConstraint.constant = 0;
            self.contentViewHeightConstraint.constant = 50;
            self.contentViewMarginTopConstraint.constant = 15;
            self.bofangImageView.hidden = NO;
            
            self.typeLabel.text = @"发布了视频";
            self.wenzhangImageView.hidden = YES;
            [self.bofangImageView sd_setImageWithURL:[NSURL URLWithString:model.cover_url] placeholderImage:[UIImage imageNamed:@"wd_gezl_lj"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
                self.wenzhangImageView.hidden = NO;
            }];
            self.neirongLabel.text = model.title;
            self.zanLabel.text = [NSString stringWithFormat:@"%@点赞", model.like_number];
            self.pinglunLabel.text = [NSString stringWithFormat:@"%@评论", model.comment_number];
            self.nicknameLabel.text = model.nickname;
            self.timeLabel.text = model.created_time;
        }
    }
    
    [self layoutIfNeeded];
    self.cellHeight = CGRectGetMaxY(self.zanLabel.frame) + 15;
    
}

- (void)addSeeMoreButton {
    __weak typeof(self) _self = self;
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:@"...全文"];
    
    YYTextHighlight *hi = [YYTextHighlight new];
    [hi setColor:[UIColor colorWithRed:0.578 green:0.790 blue:1.000 alpha:1.000]];
    hi.tapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {
        
    };
    
    [text yy_setColor:[UIColor colorWithRed:0.000 green:0.449 blue:1.000 alpha:1.000] range:[text.string rangeOfString:@"全文"]];
    [text yy_setTextHighlight:hi range:[text.string rangeOfString:@"全文"]];
    text.yy_font = self.titleLabel.font;
    
    YYLabel *seeMore = [YYLabel new];
    seeMore.attributedText = text;
    [seeMore sizeToFit];
    
    NSAttributedString *truncationToken = [NSAttributedString yy_attachmentStringWithContent:seeMore contentMode:UIViewContentModeCenter attachmentSize:seeMore.size alignToFont:text.yy_font alignment:YYTextVerticalAlignmentCenter];
    self.titleLabel.truncationToken = truncationToken;
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.headImageView.layer.cornerRadius = self.headImageView.frame.size.height / 2;
    self.headImageView.layer.masksToBounds = YES;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
